package org.maylove.wuziqi.controller;

import org.maylove.wuziqi.common.RedisUtil;
import org.maylove.wuziqi.common.ResposeMessage;
import org.maylove.wuziqi.pojo.question.Question;
import org.maylove.wuziqi.pojo.question.dto.AddAnswerDto;
import org.maylove.wuziqi.pojo.question.dto.GetQuestionDto;
import org.maylove.wuziqi.service.QuestionService;
import org.maylove.wuziqi.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    QuestionService questionService;

    @Autowired
    UserService userService;

    @Autowired
    RedisUtil redisUtil;

    Logger logger = LoggerFactory.getLogger(getClass());

    //  添加问题
    @PostMapping("/addQuestion")
    @ResponseBody
    public ResposeMessage addQuestion(@RequestHeader("token") String token, @RequestBody String question) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        Question q=new Question();
        q.setUserId((Long)(redisUtil.get(token + "_userId")));
        q.setContent(question);
        q.setAnswer("等待作者回答中...");
        questionService.addQuestion(q);
        logger.info("Player id:{} add question:{} success", ((Long) redisUtil.get(token + "_userId")), question);
        return ResposeMessage.success("success", null);
    }

    //  删除问题
    @PostMapping("/deleteQuestion")
    @ResponseBody
    public ResposeMessage deleteQuestion(@RequestHeader("token") String token, @RequestBody Long questionId) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        //  判权
        int identity = userService.getIdentity((Long)redisUtil.get(token + "_userId"));
        if(identity < 2 && !questionService.getUserId(questionId).equals((Long)redisUtil.get(token + "_userId"))) {
            return ResposeMessage.success("Insufficient user permissions!", null);
        }

        questionService.deleteQuestion(questionId);
        logger.info("Player id:{} delete question id:{} success", ((Long) redisUtil.get(token + "_userId")), questionId);
        return ResposeMessage.success("success", null);
    }

    //  查看问题
    @PostMapping("/getQuestion")
    @ResponseBody
    public ResposeMessage<List<Question> > getQuestion(@RequestHeader("token") String token, @RequestBody GetQuestionDto getQuestionDto) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        logger.info("Player id:{} get question success", ((Long) redisUtil.get(token + "_userId")));
        return ResposeMessage.success("success", questionService.getQuestion(getQuestionDto));
    }

    //  添加回答
    @PostMapping("/addAnswer")
    @ResponseBody
    public ResposeMessage addAnswer(@RequestHeader("token") String token, @RequestBody AddAnswerDto addAnswerDto) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        //  判权
        if(userService.getIdentity((Long)redisUtil.get(token + "_userId")) < 3) {
            return ResposeMessage.success("Insufficient user permissions!", null);
        }

        logger.info("Player id:{} add answer id:{} success", ((Long) redisUtil.get(token + "_userId")), addAnswerDto.getQuestionId());
        questionService.addAnswer(addAnswerDto.getQuestionId(), addAnswerDto.getAnswer());
        return ResposeMessage.success("success", null);
    }

    //  删除回答
    @PostMapping("/deleteAnswer")
    @ResponseBody
    public ResposeMessage deleteAnswer(@RequestHeader("token") String token, @RequestBody Long questionId) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        //  判权
        if(userService.getIdentity((Long)redisUtil.get(token + "_userId")) < 3) {
            return ResposeMessage.success("Insufficient user permissions!", null);
        }


        logger.info("Player id:{} add answer id:{} success", ((Long) redisUtil.get(token + "_userId")), questionId);
        questionService.deleteAnswer(questionId);
        return ResposeMessage.success("success", null);
    }

    //  修改回答
    @PostMapping("/modifyAnswer")
    @ResponseBody
    public ResposeMessage modifyAnswer(@RequestHeader("token") String token, @RequestBody AddAnswerDto addAnswerDto) {
        return addAnswer(token, addAnswerDto);
    }
}
